2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex3_masterTxMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 //
67 //
68 //******************************************************************************
69 #include "driverlib.h"
70 #include "Board.h"
71 
72 //*****************************************************************************
73 //
74 //Set the address for slave module. This is a 7-bit address sent in the
75 //following format:
76 //[A6:A5:A4:A3:A2:A1:A0:RS]
77 //
78 //A zero in the "RS" position of the first byte means that the master
79 //transmits (sends) data to the selected slave, and a one in this position
80 //means that the master receives data from the slave.
81 //
82 //*****************************************************************************
83 #define SLAVE_ADDRESS 0x48
84 
85 //*****************************************************************************
86 //
87 //Target frequency for SMCLK in kHz
88 //
89 //*****************************************************************************
90 #define CS_SMCLK_DESIRED_FREQUENCY_IN_KHZ 1000
91 
92 //*****************************************************************************
93 //
94 //SMCLK/FLLRef Ratio
95 //
96 //*****************************************************************************
97 #define CS_SMCLK_FLLREF_RATIO 30
98 
99 // Pointer to TX data
100 uint8_t TXData = 0;
101 uint8_t TXByteCtr;
102 
103 void main(void)
104 {
105  WDT_A_hold(WDT_A_BASE);
106 
107  //Set DCO FLL reference = REFO
108  CS_initClockSignal(
109  CS_FLLREF,
110  CS_REFOCLK_SELECT,
111  CS_CLOCK_DIVIDER_1
112  );
113 
114  //Set Ratio and Desired MCLK Frequency and initialize DCO
115  CS_initFLLSettle(
118  );
119 
120  //Set ACLK = VLO with frequency divider of 1
121  CS_initClockSignal(
122  CS_ACLK,
123  CS_VLOCLK_SELECT,
124  CS_CLOCK_DIVIDER_1
125  );
126 
127  //Set SMCLK = DCO with frequency divider of 1
128  CS_initClockSignal(
129  CS_SMCLK,
130  CS_DCOCLKDIV_SELECT,
131  CS_CLOCK_DIVIDER_1
132  );
133 
134  //Set MCLK = DCO with frequency divider of 1
135  CS_initClockSignal(
136  CS_MCLK,
137  CS_DCOCLKDIV_SELECT,
138  CS_CLOCK_DIVIDER_1
139  );
140 
141  // Configure Pins for I2C
142  GPIO_setAsPeripheralModuleFunctionInputPin(
143  GPIO_PORT_UCB0SCL,
144  GPIO_PIN_UCB0SCL,
145  GPIO_FUNCTION_UCB0SCL
146  );
147  GPIO_setAsPeripheralModuleFunctionInputPin(
148  GPIO_PORT_UCB0SDA,
149  GPIO_PIN_UCB0SDA,
150  GPIO_FUNCTION_UCB0SDA
151  );
152 
153  /*
154  * Disable the GPIO power-on default high-impedance mode to activate
155  * previously configured port settings
156  */
157  PMM_unlockLPM5();
158 
159  EUSCI_B_I2C_initMasterParam param = {0};
160  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
161  param.i2cClk = CS_getSMCLK();
162  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
163  param.byteCounterThreshold = 0;
164  param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP;
165  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
166 
167  //Specify slave address
168  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
170  );
171 
172  //Set Master in receive mode
173  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
174  EUSCI_B_I2C_TRANSMIT_MODE
175  );
176 
177  //Enable I2C Module to start operations
178  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
179 
180  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
181  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
182  EUSCI_B_I2C_NAK_INTERRUPT
183  );
184  //Enable master Receive interrupt
185  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
186  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
187  EUSCI_B_I2C_NAK_INTERRUPT
188  );
189  while(1)
190  {
191  __delay_cycles(1000); // Delay between transmissions
192  TXByteCtr = 4; // Load TX byte counter
193  TXData = 0;
194 
195  while (EUSCI_B_I2C_SENDING_STOP == EUSCI_B_I2C_masterIsStopSent(EUSCI_B0_BASE));
196 
197  EUSCI_B_I2C_masterSendMultiByteStart(EUSCI_B0_BASE, TXData++);
198 
199  __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
200  // Remain in LPM0 until all data
201  // is TX'd
202  // Increment data byte
203  }
204 }
205 
206 //------------------------------------------------------------------------------
207 // The USCIAB0TX_ISR is structured such that it can be used to transmit any
208 // number of bytes by pre-loading TXByteCtr with the byte count. Also, TXData
209 // points to the next byte to transmit.
210 //------------------------------------------------------------------------------
211 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
212 #pragma vector=USCI_B0_VECTOR
213 __interrupt
214 #elif defined(__GNUC__)
215 __attribute__((interrupt(USCI_B0_VECTOR)))
216 #endif
217 void USCIB0_ISR(void)
218 {
219  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
220  {
221  case USCI_NONE: // No interrupts break;
222  break;
223  case USCI_I2C_UCALIFG: // Arbitration lost
224  break;
225  case USCI_I2C_UCNACKIFG: // NAK received (master only)
226  //resend start if NACK
227  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
228  break;
229  case USCI_I2C_UCTXIFG0: // TXIFG0
230  // Check TX byte counter
231  if (TXByteCtr)
232  {
233  EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE, TXData++);
234  // Decrement TX byte counter
235  TXByteCtr--;
236  }
237  else
238  {
239  EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
240  // Exit LPM0
242  }
243  break;
244  default:
245  break;
246  }
247 }
248 
void USCIB0_ISR(void)
#define CS_SMCLK_FLLREF_RATIO
#define CS_SMCLK_DESIRED_FREQUENCY_IN_KHZ
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)